package cn.hinglo.gameclaw.client.action;

import cn.hinglo.gameclaw.admin.entity.MenuClawEntity;
import cn.hinglo.gameclaw.admin.service.ClawImageService;
import cn.hinglo.gameclaw.admin.service.ClawService;
import cn.hinglo.gameclaw.admin.service.MenuClawService;
import cn.hinglo.gameclaw.data.controller.BaseController;
import cn.hinglo.gameclaw.data.entity.Result;
import cn.hinglo.gameclaw.data.utils.CommonUtils;
import cn.hinglo.gameclaw.data.utils.FieldUtils;
import cn.hinglo.gameclaw.im.bean.LiveRoom;
import cn.hinglo.gameclaw.im.service.LiveRoomService;
import cn.hinglo.gameclaw.user.bean.Claw;
import cn.hinglo.gameclaw.user.bean.ClawImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @createTime: 2018/1/8
 * @author: HingLo
 * @description: 客户端根据分类来查询娃娃机列表
 */
@RestController
@RequestMapping("/client/cc/")
public class ClawClientAction extends BaseController {

    /**
     * 菜单分类的娃娃机服务层
     */
    private final MenuClawService menuClawService;

    /**
     * 直播地址请求
     */
    private final LiveRoomService liveRoomService;

    /**
     * 娃娃机服务层对象
     */
    private final ClawService clawService;

    private final ClawImageService clawImageService;

    /**
     * @param menuClawService 菜单分类下的娃娃机
     * @param liveRoomService 直播间服务层
     * @param clawService     娃娃机服务层
     */

    @Autowired
    public ClawClientAction(MenuClawService menuClawService, LiveRoomService liveRoomService, ClawService clawService, ClawImageService clawImageService) {
        this.menuClawService = menuClawService;
        this.liveRoomService = liveRoomService;
        this.clawService = clawService;
        this.clawImageService = clawImageService;
    }

    /**
     * 根据菜单的Id 查询娃娃机
     *
     * @param id 分类的唯一Id
     * @return 操作结果，娃娃机列表
     */
    @GetMapping("list")
    public Result listMenuClaw(@RequestParam("id") String id) {

        List<MenuClawEntity> list = menuClawService.listMenuClawService(id);
        //将数据处理成客户端需要的数据形式
        List<Map<String, Object>> mapList = new ArrayList<>(list.size());
        list.forEach(x -> {
            Map<String, Object> map = new HashMap<>(7);
            map.put("name", x.getName());
            map.put("id", x.getCid());
            map.put("roomId", x.getRoomId());
            //聊天室的创建者为
            map.put("createAt", FieldUtils.IM_ACCOUNT_ADMIN);
            map.put("gold", x.getGold());
            map.put("live1", queryLive(x.getLive1()));
            map.put("live2", queryLive(x.getLive2()));
            map.put("img", CommonUtils.imgInstall(x.getImg(), FieldUtils.IMAGE_3));
            mapList.add(map);
        });
        return success(mapList);
    }


    /**
     * 通过娃娃机的唯一ID来来查询娃娃机的信息
     *
     * @param id 娃娃机的编号
     * @return 返回结果
     */
    @GetMapping("find")
    public Result findByIdClaw(Integer id) {
        Claw claw = clawService.findByIdClawService(id);
        Map<String, Object> map = new HashMap<>(7);
        map.put("name", claw.getName());
        map.put("id", claw.getId());
        map.put("roomId", claw.getRoomId());
        //聊天室的创建者为
        map.put("createAt", FieldUtils.IM_ACCOUNT_ADMIN);
        map.put("gold", claw.getGold());
        map.put("live1", queryLive(claw.getLive1()));
        map.put("live2", queryLive(claw.getLive2()));
        map.put("img", CommonUtils.imgInstall(claw.getHeader(), FieldUtils.IMAGE_3));
        return success(map);
    }

    /**
     * 通过直播间的Id来查询直播的拉流信息
     *
     * @param liveId 直播间的Id
     * @return 拉流地址
     */
    private String queryLive(String liveId) {
        //查询直播的地址信息
        LiveRoom liveRoom = liveRoomService.queryByIdLiveRoomService(liveId);
        //根据创建时候的拉流类型的返回来返回地址
        switch (FieldUtils.LIVE_PULL_TYPE) {
            case 0:
                return liveRoom.getRtmpPullUrl();
            case 1:
                return liveRoom.getHlsPullUrl();
            case 2:
                return liveRoom.getHttpPullUrl();
            default:
                return null;
        }

    }

    /**
     * pc 端初始化获取娃娃机相关信息
     *
     * @return 结果
     */
    @PostMapping("clawInfo")
    public Result pcClawInfo(Integer id) {
        if (id == null || id < 1) {
            return error("请求娃娃机编号不正确");
        }
        Claw claw = clawService.findByIdClawService(id);
        String live1 = claw.getLive1();
        String live2 = claw.getLive2();
        Map<String, Object> map = new HashMap<>(6);
        map.put("uid_", FieldUtils.IM_ACCOUNT_ADMIN);
        map.put("token", FieldUtils.IM_PSW_ADMIN);
        map.put("password", FieldUtils.IM_PSW_ADMIN);
        map.put("room_id_", claw.getRoomId());
        map.put("push_url1_", liveRoomService.queryByIdLiveRoomService(live1).getPushUrl());
        map.put("push_url2_", liveRoomService.queryByIdLiveRoomService(live2).getPushUrl());
        return success(map);
    }

    /**
     * 大神榜 查看
     *
     * @return 返回结果
     */
    @GetMapping("okami")
    public Result okamiList() {
        return success(clawService.okamiListService());
    }

    /**
     * 查询娃娃机的详情图
     *
     * @param id 娃娃机详情图的唯一Id
     * @return 详情图列表
     */
    @GetMapping("img/list")
    public Result listClawImage(@RequestParam("id") int id) {
        List<ClawImage> list = clawImageService.listClawImageService(id);
        List<Map<String, Object>> mapList = new ArrayList<>(list.size());
        list.forEach(x -> {
            Map<String, Object> map = new HashMap<>(1);
            map.put("url", CommonUtils.imgInstall(x.getImageUrl(), FieldUtils.IMAGE_2));
            mapList.add(map);
        });
        return success(mapList);
    }
}
